<?php
/**
 * @file guise.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015年9月22日 上午11:09:26 $
 * @brief  $
 */
namespace haibao\cms\data;


class Guise extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\Guise');
    }
    
    public function getGuiseById($guiseId){
        return $this->getOneById($guiseId);
    }
    
    public function addGuise($model){
        $this->add($model);
    }
    
    public function getGuiseByCondition($userId, $guiseId){
        $sql = 'select Id,UserId,GuiseId from Guise where UserId=%s and GuiseId=%s';
        $result = $this->query($sql, array(
            $userId, $guiseId
        ));
        return $result->fetch_assoc();
    }
    
    public function getGuiseNameByUserId($userIds){
        $str = preg_replace('/(\d+)/', '%s', implode(',', $userIds));
        $sql = 'select * from Guise where GuiseId IN ('.$str.')';
        $result = $this->query($sql, $userIds);
        $dataList = array();
        while ($row = $result->fetch_assoc()){
            $dataList[$result->getData('GuiseId')] = $row;
        }
        return $dataList;
    }
    public function getUserByName($name){
        $retArr = array();
        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
        $monthAgo = date('Y-m-d',strtotime('-1 month'));
        if($name){
            $result = $this->query('select r.Id as id,r.NickName as nickname,r.UserName as username from UserReg r left join UserLogin l on r.Id=l.UserId  where r.IsActive=1 and (r.UserName like %s or r.Id=%s) and l.LastLoginTime > %s order by r.Id desc limit 20', array(
            		'%'.$name.'%',$name,$monthAgo
            ));
            
        }else{
            $result = $this->query('select Id as id,NickName as nickname,UserName as username from UserReg where IsActive=1 order by Id desc limit 20');
        }
        while ($row = $result->fetch_assoc()) {
            array_push($retArr, $row);
        }
        return $retArr;
    }
    
    public function getGuiseListByUserId($userId){
        $retArr = array();
        $sql = 'select Id,UserId,GuiseId,Username,Nickname from Guise where UserId=%s order by Id desc';
        $result = $this->query($sql, array($userId));
        
        while($row = $result->fetch_assoc()){
            array_push($retArr, $row);
        }
        return $retArr;
    }


    public function getGuiseInfoByGuiseId($guiseId){
        $retArr = array();
        $sql = 'select Id,UserId,GuiseId,Username,Nickname from Guise where GuiseId=%s limit 1';
        return $this->query($sql, array($guiseId))->fetch_assoc();
    }
    
    public function getGuiseByUserIds(array $userIds){
        $retArr = array();
        $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
        
        $str = preg_replace('/(\d+)/', '%s', implode(',', $userIds));
        $sql = 'select Id as id,NickName as nickname,UserName as username from UserReg where Id in('.$str.') order by find_in_set(Id, %s)';
        
        array_push($userIds, implode(',', $userIds));
        $result = $this->query($sql, $userIds);
        
        while($row = $result->fetch_assoc()){
            array_push($retArr, $row);
        }
        return $retArr;
    }
    
    public function deleteGuise($id){
        $this->query('delete from Guise where Id=%s limit 1', array($id));
    }
    
    public function getAppAdminInfoList(){
        $sql = 'SELECT g.UserId,u.RealName FROM Guise g LEFT JOIN `User` u ON g.UserId=u.Id GROUP BY UserId';
        $result = $this->query($sql);
        $dataList = array();
        while ($row=$result->fetch_assoc()){
            if($result->getData('RealName')){
                array_push($dataList, array(
                'id'=>$result->getData('UserId'),
                'name'=>$result->getData('RealName')
                ));
            }
        }
        return $dataList;
    }
}